package com.alliky.classify.ui.fragment

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alliky.classify.R
import com.alliky.classify.ui.adapter.ClassifyProductAdapter
import com.alliky.classify.ui.adapter.ClassifyTitleAdapter
import com.alliky.common.entity.vo.CategoryVo
import com.alliky.common.entity.vo.ProductVo
import com.alliky.common.router.Router
import com.alliky.core.base.BaseFragment
import com.alliky.home.mvp.contract.ClassifyContract
import com.alliky.home.mvp.presenter.ClassifyPresenter
import kotlinx.android.synthetic.main.fragment_classify.*

@Route(path = Router.Classify.Fragment.ClassifyFragment)
class ClassifyFragment : BaseFragment<ClassifyContract.View, ClassifyContract.Presenter>(),
    ClassifyContract.View {

    var mClassifyTitleAdapter: ClassifyTitleAdapter? = null
    var mClassifyProductAdapter: ClassifyProductAdapter? = null

    override fun createPresenter(): ClassifyContract.Presenter = ClassifyPresenter()

    override fun setLayout(): Any? = R.layout.fragment_classify

    override fun initView() {
        super.initView()

        mClassifyTitleAdapter = ClassifyTitleAdapter(mutableListOf())
        mClassifyTitleAdapter?.addChildClickViewIds(R.id.rootLayout)
        recyclerViewTitle.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mClassifyTitleAdapter
        }

        mClassifyTitleAdapter?.setOnItemChildClickListener { adapter, view, position ->
            var vo = adapter.data[position] as CategoryVo

            var list = adapter.data as MutableList<CategoryVo>

            list.forEach {
                it.isCheck = false
            }

            vo.isCheck = true

            mClassifyTitleAdapter?.notifyDataSetChanged()

        }

        mClassifyProductAdapter = ClassifyProductAdapter(mutableListOf())
        recyclerViewContent.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = mClassifyProductAdapter
        }
    }

    override fun initData() {

        var titles: MutableList<CategoryVo> = ArrayList()
        titles.add(CategoryVo("1", "男装", true))
        titles.add(CategoryVo("2", "女装", false))
        titles.add(CategoryVo("3", "百货", false))
        titles.add(CategoryVo("4", "进口", false))
        titles.add(CategoryVo("5", "数码", false))
        titles.add(CategoryVo("6", "手机", false))
        titles.add(CategoryVo("7", "电器", false))
        titles.add(CategoryVo("8", "鞋靴", false))
        titles.add(CategoryVo("9", "母婴", false))
        titles.add(CategoryVo("10", "饰品", false))
        titles.add(CategoryVo("11", "食品", false))
        titles.add(CategoryVo("12", "箱包", false))
        titles.add(CategoryVo("13", "运动", false))
        titles.add(CategoryVo("14", "洗护", false))
        titles.add(CategoryVo("15", "家装", false))
        titles.add(CategoryVo("16", "保健", false))
        titles.add(CategoryVo("17", "车品", false))
        titles.add(CategoryVo("18", "医药", false))
        mClassifyTitleAdapter?.setNewInstance(titles)

        var contents: MutableList<ProductVo> = ArrayList()
        contents.add(ProductVo("1", "笔记本", R.mipmap.icon_bijiben))
        contents.add(ProductVo("2", "轻薄本", R.mipmap.icon_phone))
        contents.add(ProductVo("4", "组装电脑", R.mipmap.icon_phone))
        contents.add(ProductVo("3", "机械键盘", R.mipmap.icon_bijiben))
        contents.add(ProductVo("5", "移动硬盘", R.mipmap.ic_launcher))
        contents.add(ProductVo("7", "游戏台式机", R.mipmap.icon_bijiben))
        contents.add(ProductVo("6", "显卡", R.mipmap.icon_phone))
        contents.add(ProductVo("8", "家用打印机", R.mipmap.icon_phone))
        contents.add(ProductVo("10", "多用途锅", R.mipmap.icon_phone))
        contents.add(ProductVo("9", "吃鸡装备", R.mipmap.icon_bijiben))
        contents.add(ProductVo("11", "豆浆机", R.mipmap.icon_bijiben))
        contents.add(ProductVo("13", "电磁炉", R.mipmap.icon_bijiben))
        contents.add(ProductVo("12", "微波炉", R.mipmap.icon_phone))
        contents.add(ProductVo("15", "压力锅", R.mipmap.icon_bijiben))
        contents.add(ProductVo("14", "电饭煲", R.mipmap.icon_phone))
        contents.add(ProductVo("16", "电水壶", R.mipmap.icon_phone))
        mClassifyProductAdapter?.setNewInstance(contents)
    }

}